local blaze = require "blaze"
local app = require "app"
local gamelib = require "club.threecard.gamelib"
local UIElement = require "blaze.ui.element"

local CONFIGS = {
    { 5, "weak", "club.threecard.slotlib/fx/common.b:chip spring weak" },
    { 50, "normal", "club.threecard.slotlib/fx/common.b:chip spring" },
    { 1000, "strong", "club.threecard.slotlib/fx/common.b:chip spring strong" },
}

local FX = gamelib.ui.element()

function FX:play()
    self:lazy("on_play")
end

function FX:on_injected()
    self:hide()
end

function FX:on_play()
    self:show()
    self.particle_system:Play(true)
end

local function get_fx(self, config)
    local ret = self[config[2]]
    if not ret then
        ret = FX.new()
        ret.res = config[3]
        ret:load()
        self[config[2]] = ret
    end
    return ret
end

local M = gamelib.ui.element()

function M:play(payout)
    app.audio.post("slotlib:payout")
    if not self.parent then
        return
    end
    for i, config in ipairs(CONFIGS) do
        if payout <= config[1] then
            local fx = get_fx(self, config)
            fx:set_parent(self.parent)
            fx:play()
            return
        end
    end
end

function M:on_release()
    for _, config in ipairs(CONFIGS) do
        local fx = self[config[2]]
        if fx then
            fx:release()
        end
    end
end

return M